<template>
  <div>
    <div class="content-header">
      <h1>系统设置<small>菜单项管理</small></h1>
      <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>系统设置</el-breadcrumb-item>
        <el-breadcrumb-item>菜单项管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="app-container">
      <div class="box">
        <div class="filter-container">
          <el-input
            placeholder="名称/关键字"
            v-model="pagination.queryString"
            style="width: 200px"
            class="filter-item"
          ></el-input>
          <el-button @click="findPageByCondition" class="dalfBut"
            >查询</el-button
          >
          <el-button type="primary" class="butT" @click="handleCreate"
            >新建</el-button
          >
        </div>
        <el-table
          size="small"
          current-row-key="id"
          :data="dataList"
          stripe
          highlight-current-row
        >
          <el-table-column
            type="index"
            align="center"
            label="序号"
          ></el-table-column>
          <el-table-column
            prop="name"
            label="菜单名称"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="linkUrl"
            label="跳转链接"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="path"
            label="地址"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="priority"
            label="排列顺序"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="parentMenuId"
            label="主菜单ID"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="level"
            label="级别"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="description"
            label="菜单备注"
            align="center"
          ></el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="mini"
                @click="handleUpdate(scope.row)"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <div class="pagination-container">
          <el-pagination
            class="pagiantion"
            @current-change="handleCurrentChange"
            :current-page="pagination.currentPage"
            :page-size="pagination.pageSize"
            layout="total, prev, pager, next, jumper"
            :total="pagination.total"
          >
          </el-pagination>
        </div>
        <!-- 新增标签弹层 -->
        <div class="add-form">
          <el-dialog title="新增菜单项" :visible.sync="dialogFormVisible">
            <el-form
              ref="dataAddForm"
              :model="formData"
              :rules="rules"
              label-position="right"
              label-width="100px"
            >
              <el-row>
                <el-col :span="12">
                  <el-form-item label="菜单名称" prop="name">
                    <el-input v-model="formData.name" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="跳转链接" prop="linkUrl">
                    <el-input v-model="formData.linkUrl" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="地址" prop="path">
                    <el-input v-model="formData.path" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="排列顺序" prop="priority">
                    <el-input v-model="formData.priority" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="图标" prop="icon">
                    <el-input v-model="formData.icon" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="主菜单ID" prop="parentMenuId">
                    <el-input v-model="formData.parentMenuId" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="菜单级别">
                      <el-select v-model="formData.level">
                        <el-option label="主菜单" value="1"></el-option>
                        <el-option label="二级菜单" value="2"></el-option>
                        <el-option label="三级菜单" value="3"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
              </el-row>
              <el-row>
                <el-col :span="24">
                  <el-form-item label="菜单备注">
                    <el-input
                      v-model="formData.description"
                      type="textarea"
                    ></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible = false">取消</el-button>
              <el-button type="primary" @click="handleAdd('dataAddForm')">确定</el-button>
            </div>
          </el-dialog>
        </div>

        <!-- 编辑标签弹层 -->
        <div class="add-form">
          <el-dialog title="编辑菜单" :visible.sync="dialogFormVisible4Edit">
            <el-form
              ref="dataEditForm"
              :model="formData"
              :rules="rules"
              label-position="right"
              label-width="100px"
            >
              <el-row>
                <el-col :span="12">
                  <el-form-item label="菜单名称" prop="name">
                    <el-input v-model="formData.name" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="跳转链接" prop="linkUrl">
                    <el-input v-model="formData.linkUrl" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="地址" prop="path">
                    <el-input v-model="formData.path" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="排列顺序" prop="priority">
                    <el-input v-model="formData.priority" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="图标" prop="icon">
                    <el-input v-model="formData.icon" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="主菜单ID" prop="parentMenuId">
                    <el-input v-model="formData.parentMenuId" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="菜单级别">
                      <el-select v-model="formData.level">
                        <el-option label="主菜单" value="1"></el-option>
                        <el-option label="二级菜单" value="2"></el-option>
                        <el-option label="三级菜单" value="3"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
              </el-row>
              <el-row>
                <el-col :span="24">
                  <el-form-item label="菜单备注">
                    <el-input
                      v-model="formData.description"
                      type="textarea"
                    ></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible4Edit = false"
                >取消</el-button
              >
              <el-button type="primary" @click="handleEdit('dataEditForm')">确定</el-button>
            </div>
          </el-dialog>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pagination: {
        //分页相关模型数据
        currentPage: 1, //当前页码
        pageSize: 5, //每页显示的记录数
        total: 1000, //总记录数
        queryString: null, //查询条件   双向绑定
      },
      dataList: [], //当前页要展示的分页列表数据
      formData: {}, //表单数据  添加检查项  formData提交给后台
      dialogFormVisible: false, //增加表单是否可见
      dialogFormVisible4Edit: false, //编辑表单是否可见
      rules: {
        //校验规则
        name: [
          { required: true, message: "菜单名称为必填项", trigger: "blur" },
          { min: 2, max: 10, message: "长度在 2 到 10个字符", trigger: "blur" },
        ],
        linkUrl: [
          { required: true, message: "菜单链接为必填项", trigger: "blur" },
          { min: 2, max: 20, message: "长度在 2 到 20个字符", trigger: "blur" },
        ],
      },
    };
  },
  //钩子函数，VUE对象初始化完成后自动执行
  created() {
    this.findPage();
  },
  methods: {
    //新建弹窗
    handleCreate() {
      this.dialogFormVisible = true;

    },
    // 条件分页查询
    findPageByCondition(){
        this.pagination.currentPage = 1;
        this.findPage()
    },
    //页码变化
    handleCurrentChange(curpage){
        this.pagination.currentPage = curpage;
        this.findPage()
    },
    //分页查询
    findPage() {
      this.$http.post("api/system/findPageMenu", this.pagination).then((res) => {
        if (res.data.flag) {
          this.pagination.total = res.data.data.total;
          this.dataList = res.data.data.rows;
          //  this.dataList=res.data.data
          //  alert(JSON.stringify(res.data.data))
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
    //编辑回显
    handleUpdate(row){
        this.dialogFormVisible4Edit = true;
        // this.formData = row;
        let rowdata = JSON.stringify(row);
        this.formData = JSON.parse(rowdata)
    },
    //编辑和新建确认
    handleAdd(rulesForm){
        this.handleEdit(rulesForm);
        this.dialogFormVisible = false;
        this.findPage();
    },
    handleEdit(rulesForm){
        this.$refs[rulesForm].validate((valid)=>{
            if(valid){
                this.$http.post("api/system/saveOrUpdateMenu",this.formData).then((res)=>{
                    if(res.data.flag){
                        this.$message.success("提交成功！");
                        this.dialogFormVisible4Edit = false;
                        this.findPage();
                    }else{
                        this.$message.error("提交失败！")
                    }
                }) 
            }else{
              this.$message.error("填写有误，请重新填写！")
            }
        })
        
    },
    handleDelete(row){
        this.$confirm('此操作将永久删除该条目, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            if(this.pagination.total%this.pagination.pageSize==1){
                this.pagination.currentPage = this.pagination.currentPage-1;
            }
            this.$http.post("api/system/deleteMenu/"+row.id).then((res)=>{
                if(res.data.flag){
                        this.$message.success("删除成功！");
                        this.dialogFormVisible4Edit = false;
                        this.findPage();
                }else{
                        this.$message.error("删除失败！")
                    }
            })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
    })}
  },
};
</script>